// miniprogram/pages/index/index.js
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');

var qqmapsdk;
const {
  mainViewPic
} = require('../../api/api')
const {
  checkLogin
} = require('../../utils/util')
const {
  decodeUserInfo
} = require('../../api/login')


const {
  flowList
} = require('../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    limit: 10,
    navHeight: getApp().globalData.navHeight,
    navTop: getApp().globalData.navTop,
    menuHeight: getApp().globalData.menuHeight,
    loginVisible: true,
    windowHeight: getApp().globalData.windowHeight,
    currentIndex: 0,
    list: [],
    total: 0,
    mainHeight: getApp().globalData.mainHeight,
    token: '',
    isPopupVisible: false,
    licensePlateNum: '',
    customer: '',
    flowCode: '',
    createTimeBegin: '',
    createTimeEnd: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    wx.hideTabBar()
    getApp().editTabbar()

    this.loadData()

  },
  onShow() {
    checkLogin('none').then((res) => {
      const {
        token
      } = this.data
      if (token === wx.getStorageSync('token')) {
        return
      }
      this.setData({
        token: wx.getStorageSync('token'),
        page: 1,
        list: []
      })
      this.loadData()
    }).catch((e) => {
      wx.showModal({
        title: '提示',
        content: '请先登录，以便获取数据',
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/index',
            })
          }
        }
      })
    })
  },
  async loadData() {
    checkLogin('none').then(async (res) => {
      const result = await flowList(params)
      if (result.length < 1) {
        return
      }
      this.setData({
        ['list[' + (page - 1) + ']']: result,
        page: page + 1,
        total: result.length
      })
    })
    const {
      page,
      limit,
      currentIndex,
      licensePlateNum,
      customer,
      flowCode,
      createTimeBegin,
      createTimeEnd
    } = this.data
    const params = {
      page,
      limit,
      userId: wx.getStorageSync('userInfo').id,
      auditState: 'A',
      transportstate: currentIndex === 0 ? 'onPassage' : 'arrive',
      licensePlateNum: licensePlateNum,
      customer: customer,
      flowCode: flowCode,
      createTimeBegin: createTimeBegin,
      createTimeEnd: createTimeEnd
    }
  },
  changeIndex(e) {
    const index = e.currentTarget.dataset.index
    this.setData({
      currentIndex: parseInt(index),
      list: [],
      page: 1,
      total:0
    })
    this.loadData()
  },
  hideModal(e) {
    this.setData({
      loginVisible: false
    })
  },
  goto(e) {
    const {
      index,
      type,
      url
    } = e.currentTarget.dataset
    if (type === 'nav') {
      if (index === 6) {
        if (!this.checkSubject()) {
          return
        }
      }
      wx.navigateTo({
        url: url,
      })
    } else if (type === 'tab') {
      wx.switchTab({
        url: url,
      })
    }
  },

  scroll(e) {
    const scrollTop = e.detail.scrollTop

    if (scrollTop > 80) {
      this.setData({
        isBack: true
      })
    } else {
      this.setData({
        isBack: false
      })
    }
  },

  inputChange(e) {
    this.setData({
      customer: e.detail.value
    })
  },
  searchData(){
    // console.log("searchData:"+this.data.customer);
    this.setData({
      isPopupVisible: true
    });
  },
  onReachBottom(){
    console.log("onReachBottom...");
    this.loadData();
  },
  refreshList(){
    this.setData({
      list: [],
      page: 1,
      total:0
    })
    this.loadData()
  },
  searchCondition(e){
    console.log("e：" + JSON.stringify(e.detail));
    this.setData({
      licensePlateNum: e.detail.licensePlateNum,
      flowCode: e.detail.flowCode,
      customer: e.detail.customer,
      createTimeBegin: e.detail.startTime,
      createTimeEnd: e.detail.endTime,
      list: [],
      page: 1,
      total:0
    });
    this.loadData();
  }

})